################################################################################
# Title   : Replication Code — Union Spillovers Paper
# Project : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
# Authors : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
# Output  : Appendix Figure 3
# Date    : August 2025
################################################################################



# union data
union.dat <- read_dta(paste0(wd,"/cps/union_city_trends.dta")) 

# for fan plot of union density

city <- union.dat %>%
  # clean labels
  mutate(
    city = as_factor(city_plus2, levels = c("default")),
    city = str_replace_all(city, "--", "-"),
    city = str_squish(city)
  ) %>%
  filter(!year %in% c(1981, 1982)) %>%
  group_by(city) %>%
  # moving average
  mutate(
    U =  rollapply(union, 3, mean, align = 'right', partial = TRUE),
    U2 =  rollapply(union, 5, mean, align = 'center', partial = TRUE)
  ) %>%
  mutate(union.declines = U - mean(U[year == 1980]))

# get city ranks in terms of union decline
city2 <- city %>%
  mutate(B = last(U) - first(U)) %>%
  slice(1) %>%
  ungroup() %>%
  arrange(B) %>%
  mutate(r = min_rank(desc(B))) %>%
  select(city, r, B)


# select cities
select.cities <- c("Detroit,MI","Gary-Hammond-East Chicago, IN","Cleveland,OH","Akron,OH",
                   "Rochester, NY", "Nassau-Suffolk, NY","Washington, DC-MD", "Sacramento, CA",
                   "Pittsburgh, PA","Chicago,IL","OH","PA","Dallas-, TX"
)

fig.2 <- city %>% 
  inner_join(city2, by = c("city"))   %>% 
  filter(str_length(city) > 5, (r < 50 | r > 60)) %>% 
  mutate(Year = as.Date(ISOdate(year, 1, 1)))  

ggplot(fig.2 %>%
         group_by(city) %>% 
         mutate(count = n()) %>% 
         filter(count > 20) %>% 
         ungroup()) + 
  geom_line(aes(y = U2, x = Year, group = city), color = "grey", alpha =2/5, show.legend = F) +
  geom_line(data = fig.2 %>% 
              filter(city %in% select.cities & !city %in% c("Chicago,IL","Pittsburgh, PA") ),
            mapping = aes(y = U2, x = Year, color = city), linewidth = .8, linetype = 1)  + 
  geom_point(data = fig.2 %>% 
               filter(city %in% select.cities & !city %in% c("Chicago,IL","Pittsburgh, PA"),
                      (year %in% seq(1980,2019,5))),
             mapping = aes(y = U2, x = Year, color = city, shape = city)) +  
  geom_line(data = city %>% 
              mutate(Year = as.Date(ISOdate(year, 1, 1))) %>% 
              group_by(Year) %>% 
              summarise(MU = weighted.mean(U, n)),
            mapping = aes(y = MU, x = Year), size = 1.5, linetype = 2)  + 
  theme(legend.key = element_rect(colour = NA, fill = NA),
        legend.title = element_blank(),
        legend.position = c(.60,.90),
        legend.background=element_blank(),
        legend.box.background = element_rect(colour = "gray"),
        legend.text = element_text(size=8)) +
  guides(color=guide_legend(ncol=3)) +
  scale_x_date(date_breaks = '5 years', date_labels = "%Y", limits = c(as.Date('1980-1-1'),NA),
               expand = expansion(mult = 0, add = 0)) +
  labs(y = "Proportion union" )

ggsave(paste0(figs,"/Appendix_Figure_3.pdf"),
       units = c("in"),
       height = 6,
       width = 8.5)



